﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using outREACH.App_Code;

namespace outREACH
{
    public partial class ContactUs_Mem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                Label lbl1 = (Label)Master.FindControl("Label1");
                lbl1.Text = "Welcome " + Session["Username"].ToString() + "!";
                Label lbl2 = (Label)Master.FindControl("Name");
                Users u = Users.GetName(Session["Username"].ToString());
                lbl2.Text = u.firstName + " " + u.lastName;
                Label lbl3 = (Label)Master.FindControl("Country");
                lbl3.Text = Session["country"].ToString();
                Button btn3 = (Button)Master.FindControl("Button4");
                if (User.IsInRole("admin"))
                {
                    btn3.Visible = true;
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            List<Users> result = Users.GetUser();
            foreach (Users u in result)
            {
                if (u.userName.ToString() == Session["UserName"].ToString())
                {
                    MailMessage message = new MailMessage();
                    message.From = new MailAddress(u.email.ToString(), u.userName);
                    message.To.Add(new MailAddress("outreach360@gmail.com"));

                    message.Subject = DropDownList2.Text;
                    message.Body = "Dear Outreach, " + u.firstName.ToString() + " " + u.lastName.ToString() + " has a question to ask regarding the " + DropDownList2.SelectedValue.ToString() + " section!" + "\n" + "\n" + TextBox12.Text;

                    SmtpClient smtp = new SmtpClient();

                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new System.Net.NetworkCredential("outreach360@gmail.com", "360outreach");

                    smtp.Send(message);

                    ContactUs cu = new ContactUs();
                    cu.dateTime = DateTime.Now;
                    cu.firstName = u.firstName.ToString();
                    cu.lastName = u.lastName.ToString();
                    cu.company = "";
                    cu.email = u.email.ToString();
                    cu.phone = u.hp;
                    cu.section = DropDownList2.SelectedValue.ToString();
                    cu.statement = TextBox12.Text.ToString();
                    ContactUs.InsertContactUs(cu);
                    DropDownList2.ClearSelection();
                    TextBox12.Text = "";
                    Label4.Visible = true;
                }
            }
        }
    }
}